/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dao;

import com.entidad.Entity;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Usuario7
 */
public class EntityDAO {
    private EntityManagerFactory emf = null;
    private EntityManager em;
    
    public EntityDAO(){
        emf = Persistence.createEntityManagerFactory("sventasPU");
        em = emf.createEntityManager();
    }
    
    public boolean insertarEntity(Entity t){
        boolean b = false;
        try {
            em.getTransaction().begin();
            em.persist(t);
            em.getTransaction().commit();
            b = true;
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        }
        return b;
    }
    
    public Entity buscarEntity(Integer id){
        Entity t = null;
        try {
            t = em.find(Entity.class, id);
        } catch(Exception e){
            e.printStackTrace();
        }
        return t;
    }
    
    public List<Entity> getListEntity(){
        Query q = em.createQuery("Select t from Entity t");
        return q.getResultList();
    }
    
    public boolean eliminarEntity(Integer id){
        boolean b = false;
        try{
            em.getTransaction().begin();
            Entity t = em.find(Entity.class, id);
            em.remove(t);
            em.getTransaction().commit();
            b = true;
        } catch (Exception e){
            e.printStackTrace();
            em.getTransaction().rollback();
        }
        return b;
    }
    
    public void closeEntityManager(){
        em.close();
    }
}